<?
Header("Content-type: image/png");
session_start();
/*
 * Describle of file class codes
 * Project:   A PHP class for creating and managing form CAPTCHA images
 */
define('char_nums',5);  //the length of captcha ;
define('circle_nums',8);  //the length of circle ;

class codes {
	/*
	 * Load the background image form images 1 to 11
	 * Create the $im as a png image
	 */
	var $im = '';
	function __construct(){
		/*
		 * Create the image Resource
		 */
		$i=rand(1,9);
		$im = imagecreatefrompng('images/'.$i.'.png');
		$this->setim($im);
	}
	/*
	 * End of Create Resource
	 * Create image from the Resource
	 */
	function Image_create(){
		/*
		 * create the array with nums and letters
		 */
		$arr=array();
		$letter=range('a','z');
		for($i=26;$i<34;$i++){
			$arr[$i]=$i-24;
		}
		$arr+=$letter;

		/*
		 * create array end
		 *
		 * add  circles start
		 * $b: new size of picture
		 * $x: circles x, $y: circles y
		 */
		$im=$this->getim();
		$image = imagecreatefrompng('images/c.png');

		for($i=0;$i<circle_nums;$i++){
			$circle_x=rand(5,150);
			$circle_y=rand(2,20);
			$size=rand(10,28);
			imagecopyresized($im, $image, $circle_x, $circle_y, 0, 0,$size,$size,36,36);
		}
		/*
		 * add circles end
		 * add chars start
		 * $x: chars x , $y: chars y
		 * $te: chars x ,$color: colors of chars
		 * $captcha: captcha of this page;
		 */
		$x=rand(5,30);
		$captcha='';
		for($i=0;$i<char_nums;$i++){
			$t=rand(0,33);
			$chars=$arr[$t];
			$te=rand(18,27);
			$x+=$te;
			$y=rand(28,40);
			$R=rand(100,255); $G=rand(0,50);$B=rand(122,255);

			$font_color=ImageColorAllocate($im, $R,$G,$B);

			imagettftext($im,20,0,$x,$y,$font_color,'font.ttf',$chars);
			$captcha.=$chars;
		}
		/*
		 * add chars end
		 * set session captcha as $chars
		 */

		$_SESSION['captcha']=$captcha;

		Imagepng($im);

		ImageDestroy($im);
	}

	function setim($im){
		$this->im=$im;
	}
	function getim(){
		return $this->im;
	}

}
?>